/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_02_oop.Example_01_Employee.java]
 * Creation Date 	[15-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_02_oop;

import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 采用面向对象的方式自定义一个类
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_01_Employee {

	// 属性定义
	private final String name;
	private double salary;
	private Date hireDate;
	// 自动生成ID
	private static int nextId = 1;
	private int id;

	// 构造方法定义
	/**
	 * 无参数构造方法
	 */
	public Example_01_Employee() {
		super();
		this.name = "";
	}

	/**
	 * 含参数构造方法
	 * 
	 * @param name
	 *            雇员姓名
	 * @param salary
	 *            雇员薪资
	 * @param year
	 *            雇佣年
	 * @param month
	 *            雇佣月
	 * @param day
	 *            雇佣日
	 */
	public Example_01_Employee(String name, double salary, int year, int month,
			int day) {
		super();
		// The keyword this refers to the implicit parameter
		this.name = name;
		this.salary = salary;
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		this.hireDate = calendar.getTime();
		this.setId();
	}

	// 方法定义
	/**
	 * 获得员工的姓名
	 * 
	 * @return 员工的姓名
	 */
	public String getName() {
		return name;
	}

	/**
	 * 获得员工的薪水
	 * 
	 * @return 员工的薪水
	 */
	public double getSalary() {
		return salary;
	}

	/**
	 * 设置员工的薪水
	 * 
	 * @param salary
	 *            薪水
	 */
	public void setSalary(double salary) {
		this.salary = salary;
	}

	/**
	 * 获得员工的雇佣日期
	 * 
	 * @return 员工的雇佣日期
	 */
	public Date getHireDate() {
		// 一般情况下如果返回的一个可变对象的引用, 那么应该对其使用clone操作
		return (Date) hireDate.clone();
	}

	/**
	 * 设置员工的雇佣日期
	 * 
	 * @param hireDate
	 *            雇佣日期
	 */
	public void setHireDate(Date hireDate) {
		this.hireDate = hireDate;
	}

	/**
	 * 增加员工薪水
	 * 
	 * @param byPercent
	 *            增长的百分比
	 */
	public void raiseSalary(double byPercent) {
		salary += salary * byPercent / 100;
	}

	private void setId() {
		this.id = nextId++;
	}

	public int getId() {
		return this.id;
	}

	/**
	 * 返回员工的字符串表示
	 * 
	 * @return 员工的字符串表示
	 */
	@Override
	public String toString() {
		return "Id " + this.id + "\tname " + this.name + "\tsalary "
				+ this.salary + "\thiredate " + this.hireDate;
	}
}
